<?php

$thisPage = "list_services";

$params = getRequestParameter("params");
$params = unserialize(stripslashes(getRequestParameter("params")));

if (!isset($params) || $params == "") {
    $params = $_POST;
}

addDataservice("getlogo");
addHtmlService("getlogo");

loadJQueryPlugin("innerfade");
loadJQueryPlugin("servicedropdown");

$pageinfo = $params['pageinfo'];

$serviceList = array();

$serviceList[] = "Accounting, Auditing";
$serviceList[] = "Advertising and PR agencies";
$serviceList[] = "Air tickets, Airlines, Travel agencies";
$serviceList[] = "Art, Entertainment";
$serviceList[] = "Attorneys";
$serviceList[] = "Banks, Insurance companies";
$serviceList[] = "Beauty salons, Skincare, Hair, Cosmetics";
$serviceList[] = "Car importers, Fleet management";
$serviceList[] = "Debt management, Security, Telecommunication";
$serviceList[] = "Computers, Software, Printing, Office supply";
$serviceList[] = "Conference centers";
$serviceList[] = "Courier and messenger services";
$serviceList[] = "Customs agents";
$serviceList[] = "Event organizers, Catering, Hostess services";
$serviceList[] = "Sport, Fitness clubs";
$serviceList[] = "Florists, Gardening, Cleaning services";
$serviceList[] = "Furniture, home decoration";
$serviceList[] = "Healthcare and pharmacies";
$serviceList[] = "Hotels";
$serviceList[] = "Logistic services, Relocation";
$serviceList[] = "Market research, Translation";
$serviceList[] = "Real estate";
$serviceList[] = "Recruiting and HR services";
$serviceList[] = "Restaurants";
$serviceList[] = "Schools and universities, Books";
$serviceList[] = "Shopping";
$serviceList[] = "Taxi, Car rental";

TemplateHelper::useLayout("tile");

addDataservice("getpage");
addHtmlService("getpage");

Log::info("Loading page functions: $thisPage");

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "getpage" : { return execute_getpage(); break; }
        case "getlogo" : { return service_getlogo($params); break; }
    }
}

function service_getlogo($params) {
    Database::loadModel("files");
    Database::getModel()->retrieveImageById($params['file_id']);
}

function buildLayout(&$smarty) {
    global $thisPage;
    global $params;
    global $serviceList;

    Log::debug("Executing function: buildLayout");

    Log::debug("storing last search");
    storeParameter("lastservicesearch", $params);

    $result = execute_getresult($params);
    Database::loadModel("companies");
    $companies = Database::getModel()->getRotatorCompaniesFor("service");
    $pictures = array(
        array(src => "site/layout/searchscreen/resources/img/agendarium_logo.png", href => "http://www.lancasterconsulting.hu"),
        array(src => "site/layout/searchscreen/resources/img/youcv_banner_2.jpg", href => "http://www.lancasterconsulting.hu"),
        array(src => "site/layout/searchscreen/resources/img/budapest_banner.jpg", href => "http://www.exporthungary.hu"),
        array(src => "site/layout/searchscreen/resources/img/empty_rotator.png", href => "index.php?choice=mediaoffer"),
    );
    $counter = 0;
    for ($i = 0; $i < count($companies) && $i < 4; $i++) {
        if ($i == 1) { $counter++; }
        $pictures[$counter]['src'] = callFileloaderDisplayCompanyFile("export", "rotator", $companies[$i]['company_id']);
        $pictures[$counter]['href'] = $companies[$i]['hphungarian'];
        $counter++;
        if ($counter >= 4) { break; }
    }
    $tiles = array();
    for ($i = 0; $i < count($result['result']); $i++) {
        $smarty->assign("item", $result['result'][$i]);
        $tiles[] = $smarty->fetch("pages/list_services/contentitem.tpl");
    }

    $result = execute_getresult_large($params);
    $tilesTopicSponsor = array();
    for ($i = 0; $i < count($result); $i++) {
        $smarty->assign("item", $result[$i]);
        $tilesTopicSponsor[] = $smarty->fetch("pages/list_services/contentitem.tpl");
    }
    $smarty->assign("tiles_large", $tilesTopicSponsor);

    $smarty->assign("title", $serviceList[$params['selectedService']-1]);
    $smarty->assign("pictures", $pictures);
    $smarty->assign("tiles", $tiles);
    $smarty->assign("bottomcontent", $smarty->fetch(Templatehelper::getTemplateFile($thisPage, "bottom")));
}

function execute_getresult($params) {
    global $pageinfo;

    initDatabase();
    Database::loadModel("recommended_services");
    $currentPage = ($pageinfo['current_page'] ? $pageinfo['current_page'] : 1);

    $result = array();
    $count = Database::getModel()->getServiceCount($params['selectedService']);
    $result = Database::getModel()->getByServiceType($params['selectedService'], "logo");
    return array(
        result => $result,
        resultCount => $count,
    );
}

function execute_getresult_mini($params) {
    global $pageinfo;

    initDatabase();
    Database::loadModel("recommended_services");

    $result = Database::getModel()->getMinibannerServicesByType($params['selectedService']);
    return $result;
}

function execute_getresult_large($params) {
    global $pageinfo;

    initDatabase();
    Database::loadModel("recommended_services");

    $result = Database::getModel()->getTopicSponsorServicesByType($params['selectedService']);
    return $result;
}

function execute_getpage() {
    global $pageinfo;
    global $smarty;

    $params = getStoredParameter("lastsearch");
    $result = execute_getresult($params);
    return getContent($smarty, $result);

}

function getSummary($smarty, $service) {
    global $params;
    global $thisPage;
    global $serviceList;

    $smarty->assign("summary", array(
        selectedService => $serviceList[$service-1]['text']
    ));
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "summary"));
}

function getpager($resultCount) {
    global $pageinfo;
    return array(
        currentpage => ($pageinfo['current_page'] ? $pageinfo['current_page'] : 1),
        pagecount => ceil($resultCount / 5),
    );
}

function getContent($smarty, $result) {
    global $thisPage;

    $resultHTML = "";
    for ($i = 0; $i < count($result['result']); $i++) {
        $smarty->assign("item", $result['result'][$i]);
        $resultHTML .= $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "contentitem"));
    }
    return $resultHTML;
}

function getPageHeader() {
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    return TemplateHelper::getDefaultPageFooter(array(
        type => "simple"
    ));
}

function getLeftSide($smarty) {
    global $thisPage;
    global $_CONFIG;

    $smarty->assign("leftpictures", array(
    ));
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "leftpictures"));
}

function getTop($smarty) {
    global $thisPage;

    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "toppictures"));
}

function getBottom($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "bottom"));
}
